<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/24
 * Time: 18:26
 */

class News extends M_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->nid = intval($this->input->get_post('nid'));
        $this->myfield = array(
            'content' => array(
                'ismain' => 1,
                'name' => fc_lang('描述'),
                'fieldname' => 'content',
                'fieldtype' => 'Ueditor',
                'setting' => array(
                    'option' => array(
                        'mode' => 1,
                        'divtop' => 1,
                        'height' => 300,
                        'width' => '100%'
                    ),
                )
            ),
//            'type' => array(
//                '1' => fc_lang('图书书籍'),
//                '2' => fc_lang('户学头条'),
//                '3' => fc_lang('教育头条'),
//            )
        );
        $field['content'] = $this->myfield['content'];
//        $field['type'] = $this->myfield['type'];
        $this->template->assign(array(
            'field' => $field,
        ));
    }

    public function index(){

        list($data, $total, $param)	= $this->_limit_page(); // 数据库中分页查询

        $id = (int)$param['id'];
        $this->load->library('dip');


        $this->template->assign(array(
            'list'=>$data,
            'total' => $total,
            'param' => $param,
            'auser' => $this->db->where('id', $id)->get('news')->row_array(),
            'pages'	=> $this->get_pagination(dr_url('admin/apply/main', array(
                'uid' => $id,
                'total' => $total,
                'search' => IS_POST ? 1 : $this->input->get('search')
            )), $total)
        ));
        $this->template->display('news/index.html');

    }
    public function add(){

        $data = array();

        if(IS_POST){
            $data  = $this->input->get_post('data');
            $img = $this->input->get_post("upload_img");
            if(isset($data)){
                $data['addtime'] = time();
                if(!empty($img)){
                    $data['img'] = $img;
                }
                $id = $this->db->insert('news', $data);
                if(!$id){
                    $this->admin_msg('入库失败');
                }
                // 操作成功处理附件
                $this->attachment_handle($this->uid, $this->db->dbprefix($this->mytable).'-'.$id, $this->myfield, $data);
                $this->admin_msg(fc_lang('操作成功，正在刷新...'), dr_url(APP_DIR.'/'.$this->router->class.'/index', $this->post_param), 1);
            }else{
                $this->admin_msg('提交数据不能为空');
            }

        }
        $this->template->assign(array(
            'data' => $data,
//            'error' => $error
            'name' => "资讯添加",
            'myfield' => $this->field_input($this->myfield, $data, TRUE),
        ));
        $this->template->display('news/news_add.html');
    }
    //上传图片
    public function uploadImg(){

        $config['upload_path']      = './uploadfile/news/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 100;
        $config['max_width']        = 1024;
        $config['max_height']       = 768;

        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('img'))
        {
            $error = array('error' => $this->upload->display_errors());
            $result['msg'] = $error  ;
            $result['status'] = 0;
//            print_r($error);exit;
//            $this->load->view('upload_form', $error);
        }else
        {
            $data = array('upload_data' => $this->upload->data());
            $result = array();
            $result['img_url'] =   $config['upload_path'].$data['upload_data']['orig_name'];
            $result['status'] = 1;
            echo json_encode($result);exit;
//            $this->load->view('upload_success', $data);
        }

    }

    public function edit(){
        $nid = (int)$this->input->get('nid');
        $data = $this->db->where('id',$nid)->get('news')->row_array();
//        print_r($_POST);exit;
        if(IS_POST){
            $sid = $this->input->get_post('nid');
            $where = $this->input->get_post('data');
            $img = $this->input->get_post("upload_img");
//            echo $sid;print_r($where);exit;
            if(empty($data)){
                $this->admin_msg('参数为空');
            }else{
                if(!empty($img)){
                    $where['img'] = $img;
                }
                $result = $this->db->where('id',$sid)->update('news', $where);
                if($result){
                    // 操作成功处理附件
                    $this->attachment_handle($sid, $this->db->dbprefix($this->mytable).'-'.$sid, $this->myfield, $where);
                    $this->admin_msg(fc_lang('操作成功，正在刷新...'), dr_url(APP_DIR.'/'.$this->router->class.'/index', $this->post_param), 1);
                }else{
                    $this->admin_msg('修改失败');
                }
            }

        }
        $this->template->assign(array(
            'nid'=> $this->nid,
            "name"=> "资讯修改",
            'data'=>$data,
            'myfield' => $this->field_input($this->myfield, $data, TRUE),
        ) );
        $this->template->display("news/news_edit.html");
    }
    public function newsdel(){
           $nid = $this->input->get_post('nid');
           if(!$nid){
               $this->admin_msg(fc_lang('对不起，数据被删除或者查询不存在'));
           }
           $result = $this->db->where("id", $nid)->delete("news");
           if($result){
               $this->admin_msg(fc_lang('删除成功'),dr_url('news/index'), 1);
           }else{
               $this->admin_msg(fc_lang('删除失败'));
           }


    }

    private function _where(&$select) {

        $uid = (int)$this->input->get('id');
        $uid = $uid ? $uid : $this->uid;
        $search = $this->input->get('search');
        $cache_file = md5($this->duri->uri(1).$uid.$this->uid.SITE_ID.$this->input->ip_address().$this->input->user_agent()); // 缓存文件名称

        // 存在POST提交时，重新生成缓存文件
        if (IS_POST) {
            $data = $this->input->post('data');
            $this->cache->file->save($cache_file, $data, 3600);
            $search = 1;
            unset($_GET['page']);
        }

        $select->where('id', $uid);

        // 存在search参数时，读取缓存文件
        if ($search == 1) {
            $data = $this->cache->file->get($cache_file);
            if (isset($data['start']) && $data['start'] && $data['start'] != $data['end']) {
                $data['end'] = $data['end'] ? $data['end'] : SYS_TIME;
                $select->where('logintime BETWEEN '.$data['start'].' AND '. $data['end']);
            }
        }

        $data['id'] = $uid;

        return $data;
    }
    private function  _limit_page(){
        $page = max((int)$this->input->get_post('page'),1);
        $_total = (int)$this->input->get_post('total');

        if(!$_total){
//            $select = $this->db;
//            $_param = $this->_where($select);
            $_total = $this->db->count_all_results('news');
            if(!$_total){
                return array(array(), 0);
            }
        }

        $select = $this->db;
//        $_param = $this->_where($select);
        $result = $this->db
            ->limit(SITE_ADMIN_PAGESIZE, SITE_ADMIN_PAGESIZE * ($page - 1))
            ->order_by('id desc')
            ->get('news')
            ->result_array();
//        return array($result,$_total, $_param);
        return array($result,$_total);
    }

}